*! 1.0.0 CFB 29 Sep 2002 
* 1.1.0 NJC 7 December 2000 _gbom
* 1.0.0  NJC 12 July 2000
program define _gelap2
	version 6
	
	gettoken type 0 : 0
	gettoken g 0 : 0
	gettoken eqs 0 : 0			/* "=" */
	gettoken paren 0 : 0, parse("() ")	/* "(" */

	gettoken time1 0 : 0, parse("() ") 
	gettoken time2 0 : 0, parse("() ")

	gettoken paren 0 : 0, parse("(), ")	/* ")" */
	if `"`paren'"' != ")" { 
		error 198
	}

	syntax [if] [in] [ , Format(str) ]

	quietly {
		tempvar touse diff tyme dd hh mm ss
		mark `touse' `if' `in'

		capture assert `time1' >= 0  if `touse' 
		if _rc { 
			di in r "`time1' contains invalid value(s)" 
			exit 198 
		} 
		capture assert `time2' >= 0  if `touse' 
		if _rc { 
			di in r "`time2' contains invalid value(s)" 
			exit 198 
		} 	
		capture assert `time2' >= `time1'  if `touse' 
		if _rc { 
			di in r "`time2' - `time1' must be non-negative" 
			exit 198 
		} 
		
		gen double `diff' = `time2' - `time1' if `touse'
		gen int `dd' = int(`diff') if `touse'
		gen long `tyme' = 86400*(`diff'-`dd') if `touse'
		gen int `hh' = int(`tyme'/3600) if `touse'
		gen int `mm' = int((`tyme' - `hh'*3600)/60) if `touse'
		gen int `ss' = int(`tyme' - `hh'*3600 - `mm'*60) if `touse'
		gen str15 `g' = string(`dd',"%6.0f") + ":"+ string(`hh',"%02.0f") /*
		*/ + ":" + string(`mm',"%02.0f") + /*
		*/ ":" + string(`ss',"%02.0f") if `touse' 	
	
		if "`format'" != "" { 
			capture format `g' `format' 
			if _rc { 
				noi di in bl "`format' invalid format" 
			} 
		} 	
	}
end

